package com.huatek.framework.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.huatek.base.tree.TreeEntity;
import com.huatek.base.tree.TreeUtil;
import com.huatek.framework.entity.FwSource;
import com.huatek.framework.entity.FwSystem;
import com.huatek.framework.service.SourceService;
import com.huatek.framework.tag.CommonBean;
import com.huatek.framework.tag.CutPageBean;
import com.huatek.framework.util.CommonUtil;
import com.huatek.framework.util.QueryCondition;
import com.huatek.framework.validate.BaseEntityEditor;
import com.huatek.framework.validate.ValidateFieldLength;

@Controller
@RequestMapping("/source.do")
@SessionAttributes(types = FwSource.class)
public class SourceController {
	@Autowired
	private SourceService sourceService;
	@RequestMapping(params = "actionMethod=add",method = RequestMethod.GET)
	public String add(Model model){
		model.addAttribute(new FwSource());
		return getInitDataPage(model);
	}
	private String getInitDataPage(Model model){
		model.addAttribute("parentList", sourceService.getAllSourceDataAsTree());
		return "page.source_edit";
	}
	@RequestMapping(params = "actionMethod=add",method = RequestMethod.POST)
	public String processAdd(Model model,@ModelAttribute FwSource source, BindingResult result, SessionStatus status)  {
		return this.processEdit(model,source, result, status);
	}
	@RequestMapping(params = "actionMethod=edit",method = RequestMethod.GET)
	public String edit(@RequestParam("queryId") Long id,Model model){
		FwSource source = (FwSource)sourceService.getObjectById(FwSource.class, id);
		source.getFwSystem().getName();
		model.addAttribute(source);
		return getInitDataPage(model);
	}
	@RequestMapping(params = "actionMethod=edit",method = RequestMethod.POST)
	public String processEdit(Model model,@ModelAttribute FwSource source, BindingResult result, SessionStatus status) {
		validate(source, result);
		if(result.hasErrors()){
			return getInitDataPage(model);
		}
		sourceService.saveSource(source);
		return "redirect:source.do?actionMethod=query";
	}
	@SuppressWarnings("unchecked")
	@RequestMapping(params = "actionMethod=delete",method = RequestMethod.POST)
	public String delete(@RequestParam("id") Long[] ids) throws Exception{
		List<FwSource> sources = sourceService.findDatasByIds(FwSource.class, ids);
		if(sources!=null&&sources.size()>0){
			sourceService.deleteAll(sources);
		}
		return "redirect:source.do?actionMethod=query";
	}
	@RequestMapping(params = "actionMethod=query")
	public String query(Model model, HttpServletRequest request) {
		List<CommonBean> paramterList = QueryCondition.getQueryCondition(request,FwSource.class);
		CutPageBean pageBean = QueryCondition.getQueryPageBean(request);
		model.addAttribute("queryParams",paramterList);
		model.addAttribute("pageBean", sourceService.getSourcePageBean(pageBean, paramterList));
		return "page.source_list";
	}
	@InitBinder
	protected void initBinder(WebDataBinder dataBinder) {
		//dataBinder.registerCustomEditor(FwGroup.class, "creatorGroup", new BaseEntityEditor(FwGroup.class));
		dataBinder.registerCustomEditor(FwSystem.class, "fwSystem", new BaseEntityEditor(FwSystem.class));
		dataBinder.registerCustomEditor(FwSource.class, "parent", new BaseEntityEditor(FwSource.class));
		/*	DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		CustomDateEditor dateEditor = new CustomDateEditor(fmt, true);
		dataBinder.registerCustomEditor(Date.class, "entryDate", dateEditor);
		dataBinder.registerCustomEditor(Date.class, "approvalDate", dateEditor);
		dataBinder.registerCustomEditor(Date.class, "dimissionDate", dateEditor);*/
	}

	@RequestMapping(params = "actionMethod=assign",method = RequestMethod.GET)
	public String assignAction(@RequestParam("id") Long id,Model model){
		return "redirect:operation.do?actionMethod=query&sourceId="+id+"&menuId=9187";
	}

	@RequestMapping(params = "actionMethod=frame",method = RequestMethod.GET)
	public String adjustFrame(Model model){
		return "page.adjust_frame";
	}
	@RequestMapping(params = "actionMethod=adjust",method = RequestMethod.GET)
	public String adjustSourceStyle(Model model){
		List<FwSource> list = sourceService.getAllSourceDataAsTree();
		for(int i=0;i<list.size();i++){
			FwSource source = list.get(i);
			source.setNodeLabel(source.getName());
			source.setLink("#");
		}
		TreeEntity.generalTreeNode(true, "", null,list);
		model.addAttribute("adjustSourceList",list);
		return "page.adjust_style";
	}

	@RequestMapping(params = "actionMethod=adjust",method = RequestMethod.POST)
	public String processAdjustSourceStyle(Model model,String saveString,String baseStyle) {
		List<TreeEntity> nodeList = TreeUtil.generalStyles(saveString, baseStyle);
		sourceService.updateTreeStyle(nodeList,FwSource.class);
		return "redirect:source.do?actionMethod=frame";
	}

	public static void validate(FwSource source, Errors errors) {
		if (CommonUtil.isZeroLengthTrimString(source.getName())) {
			errors.rejectValue("name", "required", "required");
		}
		if (CommonUtil.isZeroLengthTrimString(source.getStatus())) {
			errors.rejectValue("sourceCode", "required", "required");
		}
		if (CommonUtil.isZeroLengthTrimString(source.getStatus())) {
			errors.rejectValue("status", "required", "required");
		}
		if(source.getFwSystem()==null ){
			source.setFwSystem(new FwSystem());
		}
		if(source.getFwSystem().isNew()){
			source.getFwSystem().setId(Long.valueOf("1"));
		}

		ValidateFieldLength.validateMaxLength(errors, "sourceCode", 100, "maxLimitLength", "Enter overflow.");
		ValidateFieldLength.validateMaxLength(errors, "name", 120, "maxLimitLength", "Enter overflow.");
		ValidateFieldLength.validateMaxLength(errors, "description", 200, "maxLimitLength", "Enter overflow.");
	}



}
